//
// Created by leisurexi on 10/4/20.
//
#include <lib/acceptor.h>
#include "lib/common.h"
#include "lib/event_loop.h"
#include "lib/tcp_server.h"

// 连接建立之后的 callback
int onConnectionCompleted(struct tcp_connection *tcpConnection) {
    printf("connection completed\n");
    return 0;
}

// 数据读到 buffer 之后的 callback
int onMessage(struct buffer *input, struct tcp_connection *tcpConnection) {
    printf("get message from tcp connection %s\n", tcpConnection->name);
    printf("%s", input->data);

    struct buffer *output = buffer_new();
    int size = buffer_readable_size(input);
    for (int i = 0; i < size; i++) {
        buffer_append_char(output, buffer_read_char(input));
    }
    tcp_connection_send_buffer(tcpConnection, output);
    return 0;
}

// 数据通过 buffer 写完之后的 callback
int onWriteCompleted(struct tcp_connection *tcpConnection) {
    printf("write completed\n");
    return 0;
}

// 连接关闭之后的 callback
int onConnectionClosed(struct tcp_connection *tcpConnection) {
    printf("connection closed\n");
    return 0;
}

int main(int c, char **argv) {
    // 主线程 event_loop
    struct event_loop *eventLoop = event_loop_init();

    // 初始化 acceptor
    struct acceptor *acceptor = acceptor_init(SERV_PORT);

    // 初始 tcp_server，可以指定线程数目，如果线程是0，就只有一个线程，既负责 acceptor，也负责 I/O
    struct TCPserver *tcpServer = tcp_server_init(eventLoop, acceptor, onConnectionCompleted,
            onMessage, onWriteCompleted, onConnectionClosed, 0);

    tcp_server_start(tcpServer);

    event_loop_run(eventLoop);
}